//
//  MainPageViewReplaceVIew.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/12/6.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

import WebKit

class MainPageViewReplaceVIew : WKBaseViewController,WKBaseDelegate {

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.wkDelegate = self
        
        url = mainIndexArray.lastObject as! String
        
        let url1 = URL.init(string: url)
        
        DispatchQueue.main.async {

            TestNet.shared.getNetStatus(comfun: { (result) in
                if result.rawValue == "worked" {
                    print("worked")
                    
                    DispatchQueue.main.async {
                        
//                        self.urlRequestCache = NSURLRequest(url: url!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10)
                        self.urlRequestCache = NSURLRequest.init(url: url1!)

                        self.webView.load(self.urlRequestCache as URLRequest)
                        
                    }
                    
                } else {
                    
                    DispatchQueue.main.async {
                        
                        print("本地")
                        print(self.webView.bounds)
                        
//                        self.urlRequestCache = NSURLRequest.init(url: url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
                        self.urlRequestCache = NSURLRequest.init(url: url1!)

                        self.webView.load(self.urlRequestCache as URLRequest)

                    }
                    
                }
                
            })
            
        }
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        url = (navigationAction.request.url?.absoluteString)!
        
        textView.text = "\((#file as NSString).lastPathComponent):(\(#line))" + url

        print(url)
        
        if navigationAction.navigationType == WKNavigationType.linkActivated {
            
            print(mainIndexArray)

            
                if self.url.contains("?") {
                    self.url = self.url + ("&devtype=1&token=") + (token)
                } else {
                    self.url = self.url + ("?devtype=1&token=") + (token)
                }
            
//            }
            
            mainIndexArray.add(self.url)
            self.aaa(str: mainIndexArray.lastObject as! String)

            decisionHandler(.cancel)
            
        } else {
            decisionHandler(.allow)
        }

    }
    
    //url---
    func aaa(str : String) -> Void {
        
        
        let vvv = MainPageViewController()
        vvv.url = str
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vvv, animated: true)
    }
    
}


// MARK:- 实现代理方法
extension MainPageViewReplaceVIew {
    func server() {
//        aaa(str: fwqArray.lastObject as! String)
        
        self.navigationController!.popToRootViewController(animated: true)
    }
}
